// product/pages/sceneShow/sceneShow.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sceneId: "1978234551339581440",
    isDescriptionEllipsis: true,
    isAuthorizeUserinfo: false,
    isInputActive: false,
    sceneDetailInfo: null,
    commentList: [],
    theaterId: "",
    theaterList: [],
    copyList: [],
    page: {
      currentPage: 1,
      pageSize: 10,
      total: 0,
    },
    isGetCommentList: false,
    isEmpty: false,
    isLoadMore: false,
    showMore: false,
    commentInputValue: "",
    avatarUrl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    console.log('checkUserAuthorize-----------------1')
    this.checkUserAuthorize()
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on("sceneDetailDataEvent", (data) => {
      this.data.sceneId = data.detailInfo.id;
      this.getCommentList();
      this.getSceneDetail();
      this.getTheaterList()
      console.log(data);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  async getSceneDetail() {
    console.log("getSceneDetail");
    let res = await app.api.post({
      url: "theater_vx_detail",
      data: { theaterId: this.data.sceneId },
    });
    this.setData({
      sceneDetailInfo: res,
    });
    console.log(res);
  },
  //滚动到底部触发
  scrolltolower() {
    this.loadMore();
  },
  getTheaterList() {
    app.api
      .post({
        url: "theater_module_vx_query",
        data: { theaterId: this.data.sceneId },
      })
      .then((res) => {
        if (res.length > 5) {
          this.setData({
            theaterList: res.slice(0,5),
            copyList: res
          })
        } else {
          this.setData({
            theaterList: res.slice(),
            copyList: res
          });
        }
      });
  },
  // 情景剧场的去演示
  programmeDemonstration(e) {
    const { id, materialId, moduleType, customerSex, sceneName } = e.currentTarget.dataset.item
    console.log(id, materialId);
    if (moduleType === 'scene') {
      wx.navigateTo({
        url: `/pages/train/learning-link/learningLink?sceneId=${materialId}&sceneName=${sceneName}&customerSex=${customerSex}`
      })
    } else {
      wx.navigateTo({
        url: `/pages/home-page/to-demonstration/toDemonstration?id=${id}&from=theater`,
      })
    }
  },
  toggleMore() {
    if (this.data.theaterList.length > 5) {
      this.setData({
        theaterList: this.data.copyList.slice(0,5)
      })
    } else {
      this.setData({
        theaterList: this.data.copyList.slice()
      });
    }
    this.setData({
      showMore: !this.data.showMore,
    });
  },
  loadMore: function () {
    const { currentPage, total } = this.data.page;
    if (
      total > this.data.commentList.length &&
      this.data.isGetCommentList === false
    ) {
      this.setData({
        "page.currentPage": currentPage + 1,
        isLoadMore: true,
      });
      this.getCommentList();
    }
  },
  async getCommentList() {
    console.log("this.data.isGetSceneList", this.data.isGetCommentList);
    if (this.data.isGetCommentList) return;

    let { currentRepo, page, commentList } = this.data;
    const { currentPage, pageSize } = page;
    try {
      this.data.isGetCommentList = true;
      let res = await app.api.post({
        url: "theater_comment_vx_page",
        data: { theaterId: this.data.sceneId, currentPage, pageSize },
      });
      console.log("getSceneList", res);
      let isEmpty = false;
      let arr = res.list;
      let list = this.data.commentList;
      console.log("this.data", this.data);
      if (this.data.isLoadMore) {
        console.log("执行加载");
        list.push(...arr);
      } else {
        console.log("执行加载2");
        list = arr;
      }
      if (list.length <= 0) {
        isEmpty = true;
      }
      console.log("list", list);
      this.setData({
        commentList: list,
        isLoadMore: false,
        isEmpty: isEmpty,
        total: parseInt(res.total),
      });

      this.data.isGetCommentList = false;
    } catch (e) {
      console.log("error", e);
      this.data.isGetCommentList = false;
    }
  },
  sendMessage(e) {
    if (!this.data.isAuthorizeUserinfo) {
      this.sendMessageContent()
    } else {
      wx.getUserProfile({
        desc: "用于完善用户资料",
        success: (res) => {
          console.log(res);
          app.api.post({
            url: "userManage_img_update",
            data: { imgUrl: res.userInfo.avatarUrl },
          }).then(()=>{
            this.checkUserAuthorize()
          });
          this.data.avatarUrl = res.userInfo.avatarUrl
          this.data.isAuthorizeUserinfo = true;
          this.sendMessageContent()
        },
        fail:(res)=>{
          console.log(res)
          this.sendMessageContent()
        }
      });
    }
  },
  async sendMessageContent(){
    let content = this.data.commentInputValue;
    if(content === '') return;
    try {
      let res = await app.api.post({
        url: "theater_comment_create",
        data: { theaterId: this.data.sceneId, content: content },
      });
      console.log(res);
      this.setData({
        commentInputValue: "",
      });
        let commentList = this.data.commentList;
        this.getCommentList();
      
    } catch (e) {
      console.log(e);
      this.setData({
        commentInputValue: "",
      });
    }
  },
  inputEvent(e) {
    this.setData({
      commentInputValue: e.detail.value,
    });
  },
  publish() {
    console.log("publish");
  },
  getuserinfo(e) {
    console.log(e);
  },
  async checkUserAuthorize() {
    console.log('checkUserAuthorize')
    try {
      let res = await app.api.get({
        url: "userManage_img_update_check",
      });
      console.log(res);
      this.data.isAuthorizeUserinfo = !res.data
      console.log('isAuthorizeUserinfo',this.data.isAuthorizeUserinfo)
    } catch (e) {
      console.log(e);
      this.data.isAuthorizeUserinfo = false
    }
  },
  ellipsis(e) {
    console.log(e);
    this.setData({
      isDescriptionEllipsis: !this.data.isDescriptionEllipsis,
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
